package com.erp.framework.util;


import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

import com.erp.framework.types.CustomBigInteger;


public final class CustomBigIntegerConverter implements Converter {


    public CustomBigIntegerConverter() {

        this.defaultValue = null;
        this.useDefault = false;

    }


    public CustomBigIntegerConverter(Object defaultValue) {

        this.defaultValue = defaultValue;
        this.useDefault = true;

    }


    private Object defaultValue = null;


    private boolean useDefault = true;


    public Object convert(Class type, Object value) {

        if (value == null) {
            if (useDefault) {
                return (defaultValue);
            } else {
                throw new ConversionException("No value specified");
            }
        }

        if (value instanceof CustomBigInteger) {
            return (value);
        }

        try {
            return (new CustomBigInteger(value.toString()));
        } catch (Exception e) {
            if (useDefault) {
                return (defaultValue);
            } else {
                throw new ConversionException(e);
            }
        }

    }


}
